// pages/personal/personal.js
import request from "../../utils/request"

let startY = 0;
let moveY =  0;
let moveDistence = 0;


Page({

    /**
     * 页面的初始数据
     */
    data: {
      coverTransfrom :"translateY(0)",
      coverTransition:"",
      userInfo:{} , //用户信息
      recentPlaylist:[] //用户播放记录
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

  
   
    },
    handleTouchStart:function(event)
    {
      console.log(event);
      this.setData({
        coverTransition:""
      })
      startY = event.touches[0].clientY;
    },
    handleTouchMove:function(event)
    {
      moveY = event.touches[0].clientY;
      moveDistence  =moveY-startY;
      if(moveDistence<=0)
      {
        return;
      }
      if(moveDistence >=88)
      {
        moveDistence = 88;
      }
      // 动态更新coverTransfrom的状态的
      this.setData({
        // es6中的模板字符串
        coverTransfrom:`translateY(${moveDistence}rpx)`
      })

    },
    handleTouchEnd:function(event)
    {
        this.setData({
          coverTransfrom:`translateY(0rpx)`,
          coverTransition:`transform 1s linear`
        })
    },

    toLogin() {
        // 跳转
        wx.navigateTo({
          url:"/pages/login/login"  
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        //读取用户信息
        let userInfo  = wx.getStorageSync('userInfo')
        console.log(userInfo);
        // userInfo  是通过json,stringify 获取
        if(userInfo)
        {
          //更新数据
         this.setData({
          userInfo:JSON.parse(userInfo) // 取出数据
         })
        }
        this.getUserRectPlaylist(this.data.userInfo.userId)
        console.log(this.data.userInfo);
    },

  async getUserRectPlaylist(userId)
    {
        let recentPlaylistDate = await request("/user/record",
        {
          uid:userId,
          type:0
        })
        // console.log(recentPlaylistDate);
        let index= 0;
        let recentPlaylist = recentPlaylistDate.allData.splice(0,10).map(item=>
          {
            item.id = index++;
            return item;
          })
          this.setData({
            recentPlaylist
          })

          console.log(this.data.recentPlaylist);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})